/*
 * Created on 2005-4-29
 *
 * Copyright: Bluejay Networks Inc. 2005
 *
 * This software is the proprietary information of Bluejay Networks Inc. 
 * 
 * Use is subject to license terms.
 * 
 */
package com.ims.iaa;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author ShengMengBai
 *
 * This class get the UserAgent form resources\UserAgent.txt,
 * read it only once, you must call getInstance() to get the insance,
 * and then call getUserAgent() to get a random userAgent.
 */
public class UserAgentAux implements Serializable, Cloneable{
    private static final String FILE_PATH = "resources\\UserAgent.txt";
    private static long m_lastModified = -1;
    //this list only exist once, if the file is update, will refresh it.
    //but because if is static, we can't pass it by RMI,
    //so will create a nostatic list m_agentList
    private static ArrayList m_staticList = null;
    private ArrayList m_agentList = null;
    
    //the random number seed
    private Random m_randomSeed = null;
    //the max random number
    private int m_iMaxValue = 0;
    //the last getted user agent index    
    private int m_iLastIndex = -1;
    private String m_sFixedAgent = null;
    
    //new a random user agent object
    private UserAgentAux(){
        m_randomSeed = new Random();
    }
    //create a fix user agent object
    private UserAgentAux(String sAgent){
        m_sFixedAgent = sAgent;
    }
    /**
     * Get the random UserAgent instance.
     * if you call getUserAgent(), will get a random user agent.
     * @return
     */
    public static synchronized UserAgentAux getInstance(){
        readUserAgent();
        //create a random user angent.
        UserAgentAux instance = new UserAgentAux();
        instance.m_agentList = m_staticList;
        instance.m_iMaxValue = instance.m_agentList.size();
        return instance;
    }
    /**
     * based on the param, create a fixed user agent instance.
     * if you call getUserAgent(), will still return your input param.
     * @param sUserAgent, the fix user agent
     * @return
     */
    public static UserAgentAux getInstance(String sUserAgent){
        //create a fix user agent
        UserAgentAux instance = new UserAgentAux(sUserAgent);
        instance.m_sFixedAgent = sUserAgent;
        return instance;
    }
    /**
     * if the object is a random user Agent,
     * will Get the a random user agent from agent list.
     * this method will keep the UserAgent don't same as last.
     * else if not, will return the fix user agent.
     */
    public String getUserAgent(){
        //++add by changsheng 2006-9-7
        //the agent list may only have one record,then the random can not work
        if(m_iMaxValue == 1 ){
            return m_agentList.get(0).toString();
        }
        //--
        if(m_randomSeed != null){
	        int iIdx = m_randomSeed.nextInt(m_iMaxValue);
	        while(m_iLastIndex == iIdx){
	            iIdx = m_randomSeed.nextInt(m_iMaxValue);
	        }
	        m_iLastIndex = iIdx;
	        return m_agentList.get(m_iLastIndex).toString();
        }else{
            return m_sFixedAgent;
        }
    }
    /**
     * if the file is change, get the new data
     */
    private static boolean readUserAgent(){
		File file = new File(FILE_PATH);
		long lLastMdf = file.lastModified();
		//if the file is change, reget the agent list
		if(lLastMdf != m_lastModified){
		    try {
		        BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH));
		        //create a new list to save the user agent 
		        m_staticList = new ArrayList();
    		    String sLine = reader.readLine();
    		    //read to the end
    		    while(sLine != null){
    		        sLine = sLine.trim();
    		        //if the agent is blank, don't save it.
	    		    if(!sLine.equals("")){
	    		        m_staticList.add(sLine);
	    		    }
	    		    //read next line
    		        sLine = reader.readLine();
    		    }
    		    reader.close(); //ADDED BY VICTOR Jan 21 10:39 AM
    		    //if there is data in the file, we will create the instance.
    		    if(m_staticList.size() > 0){
        		    return true;
    		    }else{
    	   		    IaaUtil.log("There is not data in resources\\UserAgent.txt");
    		    }
            } catch (FileNotFoundException e) {
                IaaUtil.printStackTrace(e);
            } catch (IOException e) {
                IaaUtil.printStackTrace(e);
            }
   		    IaaUtil.log("Fatal error, can't get the user agent from resources\\UserAgent.txt");
   		    System.exit(3);
            return false;
		}
		return true;
    }
	public Object clone()  {
		try  {
			 return super.clone();
		} catch (CloneNotSupportedException e)  {
			IaaUtil.log("Clone not supported: " + e);
		}
		return null;
	}
    public static void main(String[] args) {
        UserAgentAux instance = getInstance();
        for(int i = 0; i < 1000; i++){
            System.out.println(instance.getUserAgent());
        }
    }
}
